package com.google.code.jira.script.workflow;

import java.util.Map;

import org.apache.log4j.Category;
import org.apache.log4j.Logger;

import com.opensymphony.module.propertyset.PropertySet;
import com.opensymphony.workflow.Condition;
import com.opensymphony.workflow.WorkflowException;

public class ScriptCondition extends ScriptWorkflowUtils implements Condition {

	private static final Category log = Logger.getLogger(ScriptCondition.class);

	public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException {
		// Evaluates the script and executes the method "passesCondition"
		Object returnObject = executeScript(transientVars, args, log);

		// Checks if the returned object is a boolean
		try {
			return ((Boolean) returnObject).booleanValue();
		} catch (NullPointerException e) {
			throw new WorkflowException("Script returned no object or \"null\". A boolean type was expected instead.",
					e);
		} catch (ClassCastException e) {
			throw new WorkflowException("Script returned a \"" + returnObject.getClass().getName()
					+ "\" object. A boolean type was expected instead.", e);
		}
	}
}